package com.serkol.driving.learning.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.serkol.annotation.IsLogin;
import com.serkol.bean.MyPage;
import com.serkol.bean.Result;
import com.serkol.constant.ResultConstant;
import com.serkol.driving.learning.entity.SysRole;
import com.serkol.driving.learning.entity.SysRolePermission;
import com.serkol.driving.learning.service.SysRolePermissionService;
import com.serkol.driving.learning.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@RestController
@RequestMapping("api/permission")
public class PermissionApiController {

    @Value("${upload.host}")
    private String uploadhost;


    @Autowired
    private SysRolePermissionService sysRolePermissionService;


    @Autowired
    private SysRoleService sysRoleService;










    @IsLogin
    @GetMapping("/findByPageList")
    public Result findByPageList(@RequestParam(name = "pageSize", defaultValue = "10") int pageSize, @RequestParam(name = "page", defaultValue = "0") int page) {
        Result result = new Result();
        MyPage list = sysRoleService.findByList(pageSize, page);
        if (list.getSize() > 0) {
            result.setStatus(ResultConstant.SUCSESS);
            result.setData(list);
        } else {
            result.setStatus(ResultConstant.EMPTIY);
        }
        return result;
    }

    @GetMapping("/roleDistributionList")
    @ResponseBody
    public Result roleDistributionList(@RequestParam(value = "id", defaultValue = "") long id) {

        LambdaQueryWrapper<SysRolePermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(SysRolePermission::getMenuId);
        queryWrapper.eq(SysRolePermission::getRoleId, id);
        List<Object> objects = sysRolePermissionService.listObjs(queryWrapper);
        return Result.ok(objects);
    }


    @PostMapping("/roleDistributionNav")
    @ResponseBody
    public Result roleDistributionNav(@RequestBody JSONObject data) {
        System.out.println(data);
        long roleId = data.getLongValue("roleId");
        JSONArray ids = data.getJSONArray("ids");
        List<Long> longs = ids.toJavaList(Long.class);
        QueryWrapper<SysRolePermission> deleteByQuery = new QueryWrapper<>();
        deleteByQuery.lambda().eq(SysRolePermission::getRoleId, roleId).notIn(SysRolePermission::getMenuId, longs);
        sysRolePermissionService.getBaseMapper().delete(deleteByQuery);
        Iterator<Long> iterator = longs.iterator();
        List<SysRolePermission> inserLog = new ArrayList<>();
        while (iterator.hasNext()) {

            Long next = iterator.next();
            SysRolePermission agentPermission = new SysRolePermission();
            agentPermission.setRoleId(roleId);
            agentPermission.setMenuId(next);
            inserLog.add(agentPermission);
        }
        boolean b = sysRolePermissionService.saveBatch(inserLog);


        return b ? Result.ok() : Result.failure();
    }
    @IsLogin
    @RequestMapping("edit")
    public Result edit(@RequestBody SysRole sysRole) {

        boolean b = false;
        if (sysRole.getId() != null && sysRole.getId() > 0) {
            b = sysRoleService.updateById(sysRole);
        } else {
            b = sysRoleService.save(sysRole);
        }
        return b ? Result.ok() : Result.failure(ResultConstant.FAIL);
    }
}
